package top.wilsonlv.jaguar.cloud.auth.feign;

import com.arcsoft.face.FaceFeature;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import top.wilsonlv.jaguar.cloud.auth.arcsoft.ArcFaceService;
import top.wilsonlv.jaguar.cloud.auth.sdk.dto.CredentialMatchEntity;
import top.wilsonlv.jaguar.cloud.auth.sdk.dto.CredentialMatchResult;
import top.wilsonlv.jaguar.cloud.auth.sdk.feign.RemoteArcFaceService;
import top.wilsonlv.jaguar.commons.data.encryption.util.EncryptionUtil;
import top.wilsonlv.jaguar.commons.web.response.JsonResult;

import javax.validation.constraints.NotBlank;

/**
 * @author lvws
 * @since 2022/1/6
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/feign/face")
public class RemoteArcFaceServiceImpl implements RemoteArcFaceService {

    private final ArcFaceService arcFaceService;

    @Override
    @PostMapping("/compareFace")
    public JsonResult<CredentialMatchResult> compareFace(@RequestBody CredentialMatchEntity credentialMatchEntity) {
        CredentialMatchResult result = arcFaceService.compareFace(credentialMatchEntity.getSource(), credentialMatchEntity.getTarget());
        return JsonResult.success(result);
    }

    @Override
    @PostMapping("/compareFeature")
    public JsonResult<CredentialMatchResult> compareFeature(@RequestBody CredentialMatchEntity credentialMatchEntity) {
        FaceFeature faceFeature1 = new FaceFeature(EncryptionUtil.decryptBase64(credentialMatchEntity.getSource()));
        FaceFeature faceFeature2 = new FaceFeature(EncryptionUtil.decryptBase64(credentialMatchEntity.getTarget()));
        CredentialMatchResult result = arcFaceService.compareFeature(faceFeature1, faceFeature2);
        return JsonResult.success(result);
    }

    @Override
    public JsonResult<CredentialMatchResult> compareFeatureWithFace(CredentialMatchEntity credentialMatchEntity) {
        FaceFeature faceFeature1 = new FaceFeature(EncryptionUtil.decryptBase64(credentialMatchEntity.getSource()));
        FaceFeature faceFeature2 = arcFaceService.check(credentialMatchEntity.getTarget());
        CredentialMatchResult result = arcFaceService.compareFeature(faceFeature1, faceFeature2);
        return JsonResult.success(result);
    }

    @Override
    @PostMapping("/check")
    public JsonResult<String> check(@RequestBody @NotBlank String face,
                                    @RequestParam(required = false, defaultValue = "false") Boolean multipleException) {
        FaceFeature faceFeature = arcFaceService.check(face, multipleException);
        return JsonResult.success(EncryptionUtil.encryptBase64(faceFeature.getFeatureData()));
    }

}
